package com.ruoyi.monitor.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dyun.tool.mybatis.domain.PageDomain;
import com.ruoyi.common.annotation.SysLog;
import com.ruoyi.common.enums.BusinessTypeEnum;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.support.DateRange;
import com.ruoyi.quartz.entity.SysJobLog;
import com.ruoyi.quartz.serviece.ISysJobLogService;
import lombok.AllArgsConstructor;
import net.dreamlu.mica.core.result.R;
import net.dreamlu.mica.core.utils.$;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 定时任务调度日志 控制层
 *
 * @author dy
 */
@RestController
@RequestMapping("/monitor/joblog")
@AllArgsConstructor
public class SysJobLogController extends BaseController {

    private final ISysJobLogService sysJobLogService;

    /**
     * 分页
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public R<IPage<SysJobLog>> list(SysJobLog sysJobLog, PageDomain pageDomain, DateRange dateRange) {
        IPage<SysJobLog> pages = sysJobLogService.page(startPage(pageDomain), buildQueryWrapper(sysJobLog, dateRange));
        return success(pages);
    }

    /**
     * 详情
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping("/detail")
    public R<SysJobLog> detail(Long id) {
        return success(sysJobLogService.getById(id));
    }

    /**
     * 删除
     */
    @SysLog(title = "定时任务调度日志", businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable Long[] ids) {
        return status(sysJobLogService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 条件删除
     */
    @SysLog(title = "定时任务", businessType = BusinessTypeEnum.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @DeleteMapping("/clean")
    public R clean(SysJobLog sysJobLog, DateRange dateRange) {
        return status(sysJobLogService.remove(buildQueryWrapper(sysJobLog, dateRange)));
    }

    private Wrapper<SysJobLog> buildQueryWrapper(SysJobLog sysJobLog, DateRange dateRange) {
        return new LambdaQueryWrapper<SysJobLog>()
                .like($.isNotBlank(sysJobLog.getJobName()), SysJobLog::getJobName, sysJobLog.getJobName())
                .eq($.isNotNull(sysJobLog.getJobGroup()), SysJobLog::getJobGroup, sysJobLog.getJobGroup())
                .eq($.isNotNull(sysJobLog.getStatus()), SysJobLog::getStatus, sysJobLog.getStatus())
                .like($.isNotBlank(sysJobLog.getInvokeTarget()), SysJobLog::getInvokeTarget, sysJobLog.getInvokeTarget())
                .between($.isAnyNotBlank(dateRange.getBeginTime(), dateRange.getEndTime()), SysJobLog::getCreateTime, dateRange.getBeginTime(), dateRange.getEndTime());
    }
}
